package kr.saeildamil.androidstudy_second.fragment.common;

import kr.saeildamil.androidstudy_second.R;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class WordListFragment extends ListFragment {
	String[] words;
	//String[] details;
	OnWordChangedListener mHost;
	
	public interface OnWordChangedListener{
		public void onWoedChanged(int index);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try{
			mHost = (OnWordChangedListener) activity;
		}catch(ClassCastException e){
			/*activity가 반드시 OnWordChangedListener를 구현해야 됨*/
			throw new ClassCastException("Activity must implement OnWordchangedLister");
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 words = getResources().getStringArray(R.array.word); 
		 //details = getResources().getStringArray(R.array.desc);
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				getActivity(), R.array.word,
				android.R.layout.simple_expandable_list_item_1);

		setListAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mHost.onWoedChanged(position);
		/*words = getResources().getStringArray(R.array.word); 
		String message = words[position] + ":" + details[position];
		Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();*/

	}
}
